﻿namespace AnyOffice.WebControls
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [DefaultProperty("Text"), ToolboxData("<{0}:HFTreeView runat=server></{0}:HFTreeView>")]
    public class HFTreeView : TreeView
    {
        public void BindOneTreeNode(DataView dv, string strText, string strValue, string strToolTip, string strImage, string strUrl)
        {
            base.Nodes.Clear();
            for (int i = 0; i < dv.Count; i++)
            {
                TreeNode child = new TreeNode();
                child.Text = dv[i][strText].ToString().Trim();
                child.Value = dv[i][strValue].ToString().Trim();
                if ((strToolTip != null) && (strToolTip != ""))
                {
                    child.ToolTip = strToolTip;
                }
                if ((strImage != null) && (strImage != ""))
                {
                    child.ImageUrl = strImage;
                }
                if (((strUrl != null) && (strUrl != "")) && (dv[i][strUrl] != null))
                {
                    child.NavigateUrl = dv[i][strUrl].ToString().Trim();
                }
                base.Nodes.Add(child);
            }
        }

        public void BindOneTreeNode(TreeNode node, DataView dv, string strText, string strValue, string strToolTip, string strImage, string strUrl)
        {
            if (this.ValidePara(strText, strValue))
            {
                for (int i = 0; i < dv.Count; i++)
                {
                    TreeNode child = new TreeNode();
                    child.Text = dv[i][strText].ToString().Trim();
                    child.Value = dv[i][strValue].ToString().Trim();
                    if ((strToolTip != null) && (strToolTip != ""))
                    {
                        child.ToolTip = strToolTip;
                    }
                    if ((strImage != null) && (strImage != ""))
                    {
                        child.ImageUrl = strImage;
                    }
                    if (((strUrl != null) && (strUrl != "")) && (dv[i][strUrl] != null))
                    {
                        child.NavigateUrl = dv[i][strUrl].ToString().Trim();
                    }
                    node.ChildNodes.Add(child);
                }
            }
        }

        public void BindTwoTreeNode(DataView dv, string strFirText, string strFirValue, string strSecText, string strSecValue)
        {
            this.BindTwoTreeNode(dv, strFirText, strFirValue, "", "", strSecText, strSecValue, "", "");
        }

        public void BindTwoTreeNode(DataView dvFir, string strFirText, string strFirValue, DataView dvSec, string strSecText, string strSecValue)
        {
            this.BindTwoTreeNode(dvFir, strFirText, strFirValue, "", "", dvSec, strSecText, strSecValue, "", "");
        }

        public void BindTwoTreeNode(TreeNode node, DataView dv, string strFirText, string strFirValue, string strSecText, string strSecValue)
        {
            this.BindTwoTreeNode(node, dv, strFirText, strFirValue, "", "", strSecText, strSecValue, "", "");
        }

        public void BindTwoTreeNode(DataView dv, string strFirText, string strFirValue, string strFirImage, string strSecText, string strSecValue, string strSecImage)
        {
            this.BindTwoTreeNode(dv, strFirText, strFirValue, "", strFirImage, strSecText, strSecValue, "", strSecImage);
        }

        public void BindTwoTreeNode(DataView dvFir, string strFirText, string strFirValue, string strFirImage, DataView dvSec, string strSecText, string strSecValue, string strSecImage)
        {
            this.BindTwoTreeNode(dvFir, strFirText, strFirValue, "", strFirImage, dvSec, strSecText, strSecValue, "", strSecImage);
        }

        public void BindTwoTreeNode(TreeNode node, DataView dv, string strFirText, string strFirValue, string strFirImage, string strSecText, string strSecValue, string strSecImage)
        {
            this.BindTwoTreeNode(node, dv, strFirText, strFirValue, "", strFirImage, strSecText, strSecValue, "", strSecImage);
        }

        public void BindTwoTreeNode(DataView dv, string strFirText, string strFirValue, string strFirToolTip, string strFirImage, string strSecText, string strSecValue, string strSecToolTip, string strSecImage)
        {
            this.BindTwoTreeNode(dv, strFirText, strFirValue, strFirImage, strFirToolTip, "", strSecText, strSecValue, strSecImage, strSecToolTip, "");
        }

        public void BindTwoTreeNode(DataView dvFir, string strFirText, string strFirValue, string strFirToolTip, string strFirImage, DataView dvSec, string strSecText, string strSecValue, string strSecToolTip, string strSecImage)
        {
            if (this.ValidePara(strFirText, strFirValue, strSecText, strSecValue))
            {
                base.Nodes.Clear();
                for (int i = 0; i < dvFir.Count; i++)
                {
                    TreeNode child = new TreeNode();
                    child.Text = dvFir[i][strFirText].ToString().Trim();
                    child.Value = dvFir[i][strFirValue].ToString().Trim();
                    if ((strFirToolTip != null) && (strFirToolTip != ""))
                    {
                        child.ToolTip = strFirToolTip;
                    }
                    if ((strFirImage != null) && (strFirImage != ""))
                    {
                        child.ImageUrl = strFirImage;
                    }
                    base.Nodes.Add(child);
                    for (int j = 0; j < dvSec.Count; j++)
                    {
                        TreeNode node2 = new TreeNode();
                        node2.Text = dvSec[j][strSecText].ToString().Trim();
                        node2.Value = dvSec[j][strSecValue].ToString().Trim();
                        if ((strSecImage != null) && (strSecImage != ""))
                        {
                            node2.ImageUrl = strSecImage;
                        }
                        if ((strSecToolTip != null) && (strSecToolTip != ""))
                        {
                            node2.ToolTip = strSecToolTip;
                        }
                        base.Nodes[i].ChildNodes.Add(node2);
                    }
                }
            }
        }

        public void BindTwoTreeNode(TreeNode node, DataView dv, string strFirText, string strFirValue, string strFirToolTip, string strFirImage, string strSecText, string strSecValue, string strSecToolTip, string strSecImage)
        {
            this.BindTwoTreeNode(node, dv, strFirText, strFirValue, strFirToolTip, strFirImage, "", strSecText, strSecValue, strSecToolTip, strSecImage, "");
        }

        public void BindTwoTreeNode(DataView dv, string strFirText, string strFirValue, string strFirToolTip, string strFirImage, string strFirUrl, string strSecText, string strSecValue, string strSecToolTip, string strSecImage, string strSecUrl)
        {
            if (this.ValidePara(strFirText, strFirValue, strSecText, strSecValue))
            {
                base.Nodes.Clear();
                string strB = null;
                int num = -1;
                for (int i = 0; i < dv.Count; i++)
                {
                    string str2 = dv[i][strFirValue].ToString().Trim();
                    if (str2.CompareTo(strB) != 0)
                    {
                        strB = str2;
                        TreeNode child = new TreeNode();
                        child.Text = dv[i][strFirText].ToString().Trim();
                        child.Value = dv[i][strFirValue].ToString().Trim();
                        if ((strFirImage != null) && (strFirImage != ""))
                        {
                            child.ImageUrl = strFirImage;
                        }
                        if ((strFirToolTip != null) && (strFirToolTip != ""))
                        {
                            child.ToolTip = strFirToolTip;
                        }
                        if ((strFirUrl != null) && (strFirUrl != ""))
                        {
                            child.NavigateUrl = dv[i][strFirUrl].ToString().Trim();
                        }
                        base.Nodes.Add(child);
                        num++;
                    }
                    if ((dv[i][strSecValue] != null) && (dv[i][strSecValue].ToString() != ""))
                    {
                        TreeNode node2 = new TreeNode();
                        node2.Text = dv[i][strSecText].ToString().Trim();
                        node2.Value = dv[i][strSecValue].ToString().Trim();
                        if ((strSecImage != null) && (strSecImage != ""))
                        {
                            node2.ImageUrl = strSecImage;
                        }
                        if ((strSecToolTip != null) && (strSecToolTip != ""))
                        {
                            node2.ToolTip = strSecToolTip;
                        }
                        if ((strSecUrl != null) && (strSecUrl != ""))
                        {
                            node2.NavigateUrl = dv[i][strSecUrl].ToString().Trim();
                        }
                        base.Nodes[num].ChildNodes.Add(node2);
                    }
                }
            }
        }

        public void BindTwoTreeNode(TreeNode node, DataView dv, string strFirText, string strFirValue, string strFirToolTip, string strFirImage, string strFirUrl, string strSecText, string strSecValue, string strSecToolTip, string strSecImage, string strSecUrl)
        {
            if (this.ValidePara(strFirText, strFirValue, strSecText, strSecValue))
            {
                string strB = null;
                int num = -1;
                for (int i = 0; i < dv.Count; i++)
                {
                    string str2 = dv[i][strFirValue].ToString().Trim();
                    if (str2.CompareTo(strB) != 0)
                    {
                        strB = str2;
                        TreeNode child = new TreeNode();
                        child.Text = dv[i][strFirText].ToString().Trim();
                        child.Value = dv[i][strFirValue].ToString().Trim();
                        if ((strFirImage != null) && (strFirImage != ""))
                        {
                            child.ImageUrl = strFirImage;
                        }
                        if ((strFirToolTip != null) && (strFirToolTip != ""))
                        {
                            child.ToolTip = strFirToolTip;
                        }
                        if ((strFirUrl != null) && (strFirUrl != ""))
                        {
                            child.NavigateUrl = dv[i][strFirUrl].ToString().Trim();
                        }
                        node.ChildNodes.Add(child);
                        num++;
                    }
                    if ((dv[i][strSecValue] != null) && (dv[i][strSecValue].ToString() != ""))
                    {
                        TreeNode node3 = new TreeNode();
                        node3.Text = dv[i][strSecText].ToString().Trim();
                        node3.Value = dv[i][strSecValue].ToString().Trim();
                        if ((strSecImage != null) && (strSecImage != ""))
                        {
                            node3.ImageUrl = strSecImage;
                        }
                        if ((strSecToolTip != null) && (strSecToolTip != ""))
                        {
                            node3.ToolTip = strSecToolTip;
                        }
                        if ((strSecUrl != null) && (strSecUrl != ""))
                        {
                            node3.NavigateUrl = dv[i][strSecUrl].ToString().Trim();
                        }
                        node.ChildNodes[num].ChildNodes.Add(node3);
                    }
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Width = Unit.Percentage(100.0);
            this.Height = Unit.Percentage(100.0);
            this.CssClass = "treebg";
            base.HoverNodeStyle.CssClass = "treehover";
            base.LeafNodeStyle.CssClass = "treeleafnode";
            base.NodeStyle.CssClass = "treenode";
            base.ParentNodeStyle.CssClass = "treeparent";
            base.RootNodeStyle.CssClass = "treerootnode";
            base.SelectedNodeStyle.CssClass = "treeselected";
        }

        private bool ValidePara(string strText, string strValue)
        {
            if ((((strText == null) || (strValue == null)) || (strText == "")) || (strValue == ""))
            {
                this.Parent.Page.Response.Write("<script language='javascript'>alert('strText或strValue参数至少有一个为空！')</script>");
                return false;
            }
            return true;
        }

        private bool ValidePara(string strFirText, string strFirValue, string strSecText, string strSecValue)
        {
            if (((((strFirText == null) || (strFirValue == null)) || ((strSecText == null) || (strSecValue == null))) || (((strFirText == "") || (strFirValue == "")) || (strSecText == ""))) || (strSecValue == ""))
            {
                this.Parent.Page.Response.Write("<script language='javascript'>alert('strFirText、strFirValue、strSecText、strSecValue参数至少有一个为空！')</script>");
                return false;
            }
            return true;
        }
    }
}

